package com.atwork.wuhua.base;

import android.widget.Toast;

import com.atwork.wuhua.dialog.LoadingDailog;


public class BasePresenter<V extends BaseView> {
    public static final int MOVE_UPDATA = 1;
    public static final int MOVE_LOADING = 2;
    /**
     * 绑定的view
     */
    private V mvpView;

    /**
     * 绑定view，一般在初始化中调用该方法
     */
    public void attachView(V mvpView) {
        this.mvpView = mvpView;
    }

    /**
     * 断开view，一般在onDestroy中调用
     */
    public void detachView() {
        this.mvpView = null;
    }

    /**
     * 是否与View建立连接
     * 每次调用业务请求的时候都要出先调用方法检查是否与View建立连接
     */
    public boolean isViewAttached() {
        return mvpView != null;
    }

    /**
     * 获取连接的view
     */
    public V getView() {
        return mvpView;
    }

    private static Toast toast;

    public void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(mvpView.getContext().getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public void showLoading() {
        LoadingDailog.getInstance(mvpView.getContext());
        LoadingDailog.show();
    }


    public void showLoading(String text) {
        LoadingDailog.getInstance(mvpView.getContext());
        LoadingDailog.show(text);
    }

    public void hideLoading() {
        LoadingDailog.dismiss();
    }
}
